//
//  WHStatusToolBar.swift
//  WHSinaWeiBo
//
//  Created by 王寒 on 16/4/12.
//  Copyright © 2016年 wanghan. All rights reserved.
//

import UIKit
import SnapKit

class WHStatusToolBar: UIView {

    private  var repost: UIButton!
    private  var commentBtn: UIButton!
    private  var ohYearBtn: UIButton!
    
    
    var statusViewModel: WHStatusViewModel?{
        didSet{
            repost.setTitle(statusViewModel?.respostTitle, forState: .Normal)
            commentBtn.setTitle(statusViewModel?.commentTitle, forState: .Normal)
            ohYearBtn.setTitle(statusViewModel?.ohYeahTitle, forState: .Normal)
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        steupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func steupUI(){
        //创建控件
        repost = addChildButton("转发", imageName: "timeline_icon_retweet")
        commentBtn = addChildButton("评论", imageName: "timeline_icon_comment")
        ohYearBtn = addChildButton("赞", imageName: "timeline_icon_like")
        
        //约束
        repost.snp_makeConstraints(closure: { (make) -> Void in
            make.top.left.bottom.equalTo(self)
        })
        
        commentBtn.snp_makeConstraints(closure: { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.left.equalTo(repost.snp_right)
            make.width.equalTo(repost.snp_width)
        })
        
        ohYearBtn.snp_makeConstraints(closure: { (make) -> Void in
            make.top.bottom.right.equalTo(self)
            make.left.equalTo(commentBtn.snp_right)
            make.width.equalTo(commentBtn.snp_width)
        })
        
        //创建分割空间
        let sep1 = sepView()
        let sep2 = sepView()
        //宽度和高度
        let w = 1 / UIScreen.mainScreen().scale
        let multiplied = 0.4
        
        sep1.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp_centerY)
            make.left.equalTo(repost.snp_right)
            make.width.equalTo(w)
            make.height.equalTo(self.snp_height).multipliedBy(multiplied)
            
        }
        
        sep2.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp_centerY)
            make.right.equalTo(ohYearBtn.snp_left)
            make.width.equalTo(w)
            make.height.equalTo(self.snp_height).multipliedBy(multiplied)
            
        }

        
    }
    
    private func sepView() -> UIView{
        let line = UIView()
        line.backgroundColor = UIColor.purpleColor()
        addSubview(line)
        return line
    }
    
    //方法传入参数 得到按钮
    private func addChildButton(title: String,imageName: String) -> UIButton{
        
        let btn = UIButton()
        btn.setTitle(title, forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: .Highlighted)

        btn.sizeToFit()
        addSubview(btn)
        return btn

    }
    

}
